package com.easyboot.web.monitor.jobutils;

import com.easyboot.utils.log.LoggerHelper;
import com.easyboot.web.monitor.jobs.SimpleQuartzJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Date;

/**
 * @author yushuo
 * @className
 * @descripton TODO
 * @date 2021/3/2 11:00
 **/
public class JobHelper {
   // @Autowired
    //private Scheduler scheduler;

    public void create(){
        JobDetail jobDetail = JobBuilder.newJob(SimpleQuartzJob.class)
                // 任务标识，及任务分组
                .withIdentity("job1", "group1")
                // 链接调用，增加需要的参数
                .usingJobData("name","Java旅途")
                .usingJobData("age",18)
                .build();
        SimpleTrigger simpleTrigger = TriggerBuilder.newTrigger()
                .withIdentity("trigger1", "group1")
                // 立即执行
                .startNow()
                // 10s后停止
                .endAt(new Date(System.currentTimeMillis()+100*1000))
                .withSchedule(
                        SimpleScheduleBuilder.simpleSchedule()
                                // 每秒执行一次
                                .withIntervalInMinutes(1)
                                // 一直执行
                                .repeatForever()
                ).build();

        try{

            //1.创建Scheduler的工厂
            SchedulerFactory sf = new StdSchedulerFactory();
            //2.从工厂中获取调度器实例
            Scheduler scheduler = sf.getScheduler();
            scheduler.scheduleJob(jobDetail,simpleTrigger);
            scheduler.start();
        }catch (SchedulerException e){
            LoggerHelper.console(e.getMessage());
        }

    }
}
